<?php

class RegistrationController extends Zend_Controller_Action{
	
	public function init()
	{
		$title ="Sign Up to our web site";
		$this->view->__set("headtitle", $title);
	}
	
	public function indexAction(){
		if(Zend_Auth::getInstance()->hasIdentity()){
			$this->_redirect('index');
		}
		$this->view->form = $form = new Application_Form_Registration();
		//$form->setDecorators( array( array('ViewScript', array('viewScript' => 'login/loginform2.phtml'))));
		if($this->_request->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData) AND $form->getElement("submit")->getAttrib("test") == "registration"){
				$user = new Application_Model_User($form->getValues());
				//set the user avatar from his gender.
				$avatar = "p_".$form->getValue("sex").".png";
				$user->setAvatar($avatar);
                $mapper  = new Application_Model_UserMapper();
                $user_id = $mapper->save($user);
                $baseurl = $this->getFrontController()->getBaseUrl();
                //Envoi de mail pour activation du compte
                $smtpConnection = new Zend_Mail_Transport_Smtp('smtp.gmail.com', Zend_Registry::get('Mail_Config'));
                $mail = new Zend_Mail('utf-8');
                $mail	->addTo($formData['email'])
                ->setFrom('joseph2033@gmail.com', 'L\'équipe de Youtajeer.com')
                ->setSubject('Bienvenue sur Youtajeer Email d\'activation')
                ->setBodyHtml('

<table class="ecxouter" width="100%" align="center" cellspacing="0" cellpadding="0" border="0" style="background:#ddd;">
<tbody>
	<tr>
	<td>
		<table cellpadding="10" align="center" width="720" cellspacing="0" width="100%" style="border:1px solid #d9d9d9;border-radius:8px;background:#FFF;">
		<tbody>
			<tr>
			<td class="ecxt-i" valign="top" width="50"><img src="http://'.$_SERVER['HTTP_HOST'].$baseurl.'/img/profil/'.$avatar.'" style="border:0;background-color:#f2f2f2;border-radius:5px;padding:0;display:block;" border="0" height="50" width="50"></td>
			<td class="ecxt-m" valign="top">
				<table align="left" border="0" cellpadding="0" cellspacing="0" width="85%">
				<tbody>
					<tr>
					<td> <p class="ecxt-t" style="font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;color:#333;font-size:14px;line-height:1.1;font-weight:bold;"><span dir="ltr">Bonjour '.$user->getFirstName().' '.$user->getLastName().'</span>
						 </p>
					<div class="ecxt-s">
					<p style="font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;color:#333;font-size:14px;line-height:1.25em;">
                		
                
                		Bienvenue à Youtajeer.<br>
                
                		Pour activer votre compte cliquez sur ce 
                		<a href="http://'.$_SERVER['HTTP_HOST'].$baseurl.'/registration/activate/id/'.sha1($formData['email'].$_SERVER['REMOTE_ADDR'].$formData['password']).'">LIEN D\'ACTIVATION</a> 
                		<br>
                
                		à bientôt,<br>
                		L\'équipe de Youtajeer.com<br>
					</p>
					</div> 
					
					</td>
					</tr>
				</tbody>
				</table>
				<table class="ecxdate" style="font-size:12px;width:60px;text-align:right;" align="right" border="0" cellpadding="0" cellspacing="0" width="40">
				<tbody>
					<tr>
					<td><a href="http://'.$_SERVER['HTTP_HOST'].$baseurl.'" style="border:none;color:#0084b4;text-decoration:none;font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;color:#999;" target="_blank">'.date("Y-m-d").'</a></td>
					</tr>
				</tbody>
				</table> 
			</td>
			</tr>
		</tbody>
		</table>
		<table class="ecxaction" width="720" cellspacing="0" cellpadding="0" border="0" align="center">
		<tbody>
		<tr>
		<td class="ecxmid ecxo-m" style="padding:10px;font-family:\'Helvetica Neue\', Helvetica, Arial, sans-serif;color:#333;padding:0;border-radius:5px;background-color:#ededed;padding:10px;">
			<table class="ecxo-c" border="0" cellpadding="0" cellspacing="0" width="100%">
			<tbody>
				<tr>
				<td class="ecxcut ecxsign-message" style="line-height:15px;"> <span class="ecxsuggestions_tip" style="font-size:14px;color:#333;font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;"><strong>Ne ratez rien.</strong> Restez au courant de ce qui se passe.</span></td>
				<td align="right" valign="middle">
					<table class="button ecxmedia_button" style="background-color:#33a9e5;background-position:top;border-radius:5px;border-color:#2288cc;border-style:solid;border-width:1px;text-align:center;height:28px;" bgcolor="#33a9e5" border="0" cellpadding="0" cellspacing="0">
					<tbody>
					<tr>
						<td class="spacer" style="font-size:1px;" width="10"></td>
						<td align="center" height="28"> <span class="ecxbutton_text" style="color:#fff;font-size:13px;font-weight:bold;text-shadow:0 -1px 0 #333;white-space:nowrap;font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;"><a class="ecxbutton_link" href="http://'.$_SERVER['HTTP_HOST'].$baseurl.'" style="border:none;color:#0084b4;text-decoration:none;color:#fff;" target="_blank">Aller sur Youtajeer.com</a></span></td>
						<td class="spacer" style="font-size:1px;" width="10"></td>
					</tr>
					</tbody>
					</table> 
				</td>
				</tr>
			</tbody>
			</table>
		</td>
		</tr>
		</tbody>
		</table>
	</td>
</tr>
</tbody>
</table>
                		');
                $mail->send($smtpConnection);
                $this->view->alertType = 'success';
                $this->view->alert = 'Registration success!! You must check your email for account activation';
                $form->reset();
                //return $this->_helper->redirector();
			}else{
				$form->populate($formData);
			}
		}
		
	}
	
	public function activateAction()
	{
		$token = $this->getRequest()->getParam('id');
		if($token){
			$mapper = new Application_Model_UserMapper();
			$mapper->validateUserByToken($token);
		}
		$this->_redirect('/');
	}
}